package com.jgame.secondarymarket.domain.auth.entity;

import com.jgame.secondarymarket.domain.account.entity.Account;
import lombok.Setter;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @title: AuthenticAccount
 * @Author Wangsj
 * @Date: 2021/7/27
 */
@Setter
public class AuthenticAccount extends Account implements UserDetails {

    public AuthenticAccount() {
        super();
    }

    public AuthenticAccount(Account origin) {
        this();
        BeanUtils.copyProperties(origin, this);
        //获取权限
        List<String> roles = Arrays.asList(this.getRole().split(","));
        List<SimpleGrantedAuthority> simpleGrantedAuthorities = roles.stream()
                .map(x -> new SimpleGrantedAuthority(x))
                .collect(Collectors.toList());
        authorities.addAll(simpleGrantedAuthorities);
    }

    /**
     * 该用户拥有的授权，譬如读取权限、修改权限、增加权限等等
     */
    private Collection<GrantedAuthority> authorities = new HashSet<>();

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getUsername() {
        return this.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
